package com.tjec.project.businessStandardLibrary.controller;

import java.util.List;

import com.tjec.common.constant.Constants;
import com.tjec.common.utils.StringUtils;
import com.tjec.project.businessStandardLibrary.domain.BmsKnowledgeBusinessGuide;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.tjec.framework.aspectj.lang.annotation.Log;
import com.tjec.framework.aspectj.lang.enums.BusinessType;
import com.tjec.project.businessStandardLibrary.domain.BmsKnowledgeTemplate;
import com.tjec.project.businessStandardLibrary.service.IBmsKnowledgeTemplateService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.tjec.framework.web.controller.BaseController;
import com.tjec.framework.web.domain.AjaxResult;
import com.tjec.common.utils.poi.ExcelUtil;
import com.tjec.framework.web.page.TableDataInfo;

/**
 * 模板下载Controller
 * 
 * @author pangyongfeng
 * @date 2020-04-21
 */
@Api("模板下载")
@RestController
@RequestMapping("/library/template")
public class BmsKnowledgeTemplateController extends BaseController
{
    @Autowired
    private IBmsKnowledgeTemplateService bmsKnowledgeTemplateService;
    @Value("${sftp.http}")
    private String http;
    @Value("${sftp.ip}")
    private String ip;

    /**
     * 查询模板下载列表
     */
    @ApiOperation("查询模板下载列表")
    @PreAuthorize("@ss.hasPermi('library:template:list')")
    @GetMapping("/list")
    public TableDataInfo list(BmsKnowledgeTemplate bmsKnowledgeTemplate)
    {
        startPage();
        List<BmsKnowledgeTemplate> list = bmsKnowledgeTemplateService.selectBmsKnowledgeTemplateList(bmsKnowledgeTemplate);
        for (BmsKnowledgeTemplate knowledgeTemplate : list) {
            addResourcePrefix(knowledgeTemplate);
        }
        return getDataTables(list);
    }

    /**
     * 查询模板下载列表
     */
    @ApiOperation("查询模板下载列表")
    @PreAuthorize("@ss.hasPermi('library:template:list')")
    @GetMapping("/treeList")
    public TableDataInfo treeList(BmsKnowledgeTemplate bmsKnowledgeTemplate)
    {
        startPage();
        List<BmsKnowledgeTemplate> list = bmsKnowledgeTemplateService.selectBmsKnowledgeTemplateTreeList(bmsKnowledgeTemplate);
        for (BmsKnowledgeTemplate knowledgeTemplate : list) {
            addResourcePrefix(knowledgeTemplate);
        }
        return getDataTables(list);
    }
    /**
     * 获取模板下载详细信息  详情
     */
    @ApiOperation("获取模板下载详细信息")
    //@PreAuthorize("@ss.hasPermi('library:template:query')")
    @GetMapping(value = "/details")
    public AjaxResult details(String guid)
    {
        BmsKnowledgeTemplate bmsKnowledgeTemplate = bmsKnowledgeTemplateService.selectBmsKnowledgeTemplateByGuiddetails(guid);
        addResourcePrefix(bmsKnowledgeTemplate);
        return AjaxResult.success("操作成功",bmsKnowledgeTemplate);
    }
    /**
     * 导出模板下载列表
     */
    @ApiOperation("导出模板下载列表")
    @PreAuthorize("@ss.hasPermi('library:template:export')")
    @Log(title = "模板下载", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BmsKnowledgeTemplate bmsKnowledgeTemplate)
    {
        List<BmsKnowledgeTemplate> list = bmsKnowledgeTemplateService.selectBmsKnowledgeTemplateList(bmsKnowledgeTemplate);
        ExcelUtil<BmsKnowledgeTemplate> util = new ExcelUtil<BmsKnowledgeTemplate>(BmsKnowledgeTemplate.class);
        return util.exportExcel(list, "template");
    }

    /**
     * 获取模板下载详细信息
     */
    @ApiOperation("获取模板下载详细信息")
    @PreAuthorize("@ss.hasPermi('library:template:query')")
    @GetMapping(value = "/{guid}")
    public AjaxResult getInfo(@PathVariable("guid") String guid)
    {
        BmsKnowledgeTemplate bmsKnowledgeTemplate = bmsKnowledgeTemplateService.selectBmsKnowledgeTemplateByGuid(guid);
        addResourcePrefix(bmsKnowledgeTemplate);
        return AjaxResult.success(bmsKnowledgeTemplate);
    }

    /**
     * 新增模板下载
     */
    @ApiOperation("新增模板下载")
    @PreAuthorize("@ss.hasPermi('library:template:add')")
    @Log(title = "模板下载", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BmsKnowledgeTemplate bmsKnowledgeTemplate)
    {
        return toAjax(bmsKnowledgeTemplateService.insertBmsKnowledgeTemplate(bmsKnowledgeTemplate));
    }

    /**
     * 修改模板下载
     */
    @ApiOperation("修改模板下载")
    @PreAuthorize("@ss.hasPermi('library:template:edit')")
    @Log(title = "模板下载", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BmsKnowledgeTemplate bmsKnowledgeTemplate)
    {
        return toAjax(bmsKnowledgeTemplateService.updateBmsKnowledgeTemplate(bmsKnowledgeTemplate));
    }

    /**
     * 删除模板下载
     */
    @ApiOperation("删除模板下载")
    @PreAuthorize("@ss.hasPermi('library:template:remove')")
    @Log(title = "模板下载", businessType = BusinessType.DELETE)
	@DeleteMapping("/{guids}")
    public AjaxResult remove(@PathVariable String[] guids)
    {
        return toAjax(bmsKnowledgeTemplateService.deleteBmsKnowledgeTemplateByGuids(guids));
    }

    /**
     * 附件路径添加前缀
     * @param bmsKnowledgeTemplate
     * @return
     */
    private void addResourcePrefix(BmsKnowledgeTemplate bmsKnowledgeTemplate){
        if(bmsKnowledgeTemplate!=null&& StringUtils.isNotBlank(bmsKnowledgeTemplate.getStoragePath())){
            bmsKnowledgeTemplate.setStoragePath(http + ip +Constants.RESOURCE_PREFIX + bmsKnowledgeTemplate.getStoragePath());
        }
    }
}
